package view;
/**
 *
 * @author zhaoyingxi
 */
import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.Rectangle;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPasswordField;
import java.awt.Color;
import java.awt.SystemColor;
import Control.UserControl;
import model.User;


public class LoginViewT extends JFrame {
    JPanel contentPane;
    JButton jButton1 = new JButton();
    JButton jButton2 = new JButton();
    JButton jButton3 = new JButton();
    JTextField userNameText = new JTextField();
    JTextField jTextField3 = new JTextField();
    JLabel jLabel1 = new JLabel();
    JLabel jLabel2 = new JLabel();
    JPasswordField passWordText = new JPasswordField();
    public LoginViewT() {
        try {
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            jbInit();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    /**
     * @author zhaoyingxi jc11839209
     */
    private void jbInit() throws Exception {
        contentPane = (JPanel) getContentPane();
        contentPane.setLayout(null);
        setSize(new Dimension(420, 300));
        setTitle("******Online AirLine Manager System******");
        jButton1.setBounds(new Rectangle(74, 225, 81, 23));
        jButton1.setSelectedIcon(null);
        jButton1.setText("Login");
        jButton1.addActionListener(new LoginViewT_jButton1_actionAdapter(this));
        jButton2.setBounds(new Rectangle(193, 225, 81, 23));
        jButton2.setText("Register");
        jButton2.addActionListener(new LoginViewT_jButton2_actionAdapter(this));
        jButton3.setBounds(new Rectangle(315, 225, 81, 23));
        jButton3.setMnemonic('0');
        jButton3.setText("Exit");
        jButton3.addActionListener(new LoginViewT_jButton3_actionAdapter(this));
        userNameText.setBounds(new Rectangle(256, 71, 123, 20));
        jTextField3.setText("jTextField3");
        jTextField3.setBounds(new Rectangle(172, 88, 123, 20));
        jLabel1.setText("UserName:");
        jLabel1.setBounds(new Rectangle(110, 76, 108, 15));
        jLabel2.setText("Passwd:");
        jLabel2.setBounds(new Rectangle(110, 125, 108, 15));
        passWordText.setBounds(new Rectangle(256, 120, 123, 20));
        contentPane.setBackground(SystemColor.scrollbar);
        this.setResizable(false);
        contentPane.add(jButton2);
        contentPane.add(jButton1);
        contentPane.add(jButton3);
        contentPane.add(jLabel2);
        contentPane.add(jLabel1);
        contentPane.add(userNameText);
        contentPane.add(passWordText);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    //Login Button
    public void jButton1_actionPerformed(ActionEvent e) {
       User user = UserControl.login(userNameText.getText(),passWordText.getText());
       if(user.getId()>0)
       {
           ViewContainer view = new ViewContainer(user);
           view.setVisible(true);
           this.dispose();
       }
    }
    //Register Button
    public void jButton2_actionPerformed(ActionEvent e) {
        RegisterViewT register = new RegisterViewT();
        register.setVisible(true);
//        register.setVisible(true);
    }
    //Exit Button
    public void jButton3_actionPerformed(ActionEvent e) {
        this.dispose();
    }
    public static void main(String[] args){
        LoginViewT a = new  LoginViewT();

        a.setVisible(true);
    }

}

class LoginViewT_jButton3_actionAdapter implements ActionListener {
    private LoginViewT adaptee;
    LoginViewT_jButton3_actionAdapter(LoginViewT adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jButton3_actionPerformed(e);
    }
}


class LoginViewT_jButton2_actionAdapter implements ActionListener {
    private LoginViewT adaptee;
    LoginViewT_jButton2_actionAdapter(LoginViewT adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jButton2_actionPerformed(e);
    }
}


class LoginViewT_jButton1_actionAdapter implements ActionListener {
    private LoginViewT adaptee;
    LoginViewT_jButton1_actionAdapter(LoginViewT adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jButton1_actionPerformed(e);
    }
}
